/*     */ package com.zimbra.cs.account.ldap.upgrade;
/*     */ 
/*     */ import com.zimbra.common.account.Key.AccountBy;
/*     */ import com.zimbra.common.account.Key.DistributionListBy;
/*     */ import com.zimbra.common.account.Key.DomainBy;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.EmailUtil;
/*     */ import com.zimbra.common.util.StringUtil;
/*     */ import com.zimbra.cs.account.AccessManager;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.Domain;
/*     */ import com.zimbra.cs.account.Entry.EntryType;
/*     */ import com.zimbra.cs.account.Group;
/*     */ import com.zimbra.cs.account.NamedEntry;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.account.SearchDirectoryOptions;
/*     */ import com.zimbra.cs.account.SearchDirectoryOptions.MakeObjectOpt;
/*     */ import com.zimbra.cs.account.SearchDirectoryOptions.ObjectType;
/*     */ import com.zimbra.cs.account.SearchDirectoryOptions.SortOpt;
/*     */ import com.zimbra.cs.account.accesscontrol.GranteeType;
/*     */ import com.zimbra.cs.account.accesscontrol.TargetType;
/*     */ import com.zimbra.cs.account.ldap.LdapProv;
/*     */ import com.zimbra.cs.ldap.ZLdapFilterFactory.FilterId;
/*     */ import com.zimbra.soap.admin.type.GranteeSelector.GranteeBy;
/*     */ import com.zimbra.soap.type.TargetBy;
/*     */ import java.util.HashMap;
/*     */ import java.util.HashSet;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import java.util.Set;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class BUG_66387
/*     */   extends UpgradeOp
/*     */ {
/*     */   private int numInspected;
/*     */   private int numFixed;
/*     */   
/*     */   BUG_66387()
/*     */     throws ServiceException
/*     */   {
/*  54 */     AccessManager.getInstance();
/*     */   }
/*     */   
/*     */   Description getDescription()
/*     */   {
/*  59 */     return new Description(this, new String[] { "zimbraAllowFromAddress", "zimbraPrefAllowAddressForDelegatedSender" }, new Entry.EntryType[] { Entry.EntryType.ACCOUNT, Entry.EntryType.DISTRIBUTIONLIST }, "see notes", "see notes", "Any internal account or distribution list address listed in zimbraAllowFromAddress attribute is converted to a grant of sendAs (for account) or sendAsDistList (for DL) right from the named account or DL.  The address is added to the zimbraPrefAllowAddressForDelegatedSender attribute of the granting account/DL.");
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   void doUpgrade()
/*     */     throws ServiceException
/*     */   {
/*  73 */     searchAndFixAccounts();
/*  74 */     this.printer.println("Number of accounts using zimbraAllowFromAddress: " + this.numInspected);
/*  75 */     this.printer.println("Number of accounts migrated: " + this.numFixed);
/*  76 */     this.printer.println("Migration completed");
/*     */   }
/*     */   
/*     */   private int searchAndFixAccounts() throws ServiceException {
/*  80 */     String[] attrsToGet = { "zimbraAllowFromAddress" };
/*  81 */     SearchDirectoryOptions searchOpts = new SearchDirectoryOptions(attrsToGet);
/*  82 */     searchOpts.setTypes(new SearchDirectoryOptions.ObjectType[] { SearchDirectoryOptions.ObjectType.accounts, SearchDirectoryOptions.ObjectType.resources });
/*  83 */     searchOpts.setSortOpt(SearchDirectoryOptions.SortOpt.SORT_ASCENDING);
/*  84 */     searchOpts.setSortAttr("zimbraMailDeliveryAddress");
/*  85 */     searchOpts.setMakeObjectOpt(SearchDirectoryOptions.MakeObjectOpt.NO_DEFAULTS);
/*  86 */     searchOpts.setFilterString(ZLdapFilterFactory.FilterId.LDAP_UPGRADE, "(zimbraAllowFromAddress=*)");
/*     */     
/*  88 */     List<NamedEntry> accounts = this.prov.searchDirectory(searchOpts);
/*  89 */     for (int i = 0; i < accounts.size(); i++) {
/*  90 */       NamedEntry entry = (NamedEntry)accounts.get(i);
/*  91 */       if ((entry instanceof Account)) {
/*  92 */         Account acct = (Account)entry;
/*  93 */         fixAccount(acct.getName(), acct.getAllowFromAddress());
/*     */       }
/*     */     }
/*  96 */     return accounts.size();
/*     */   }
/*     */   
/*     */   private void fixAccount(String name, String[] allowFromAddresses) throws ServiceException {
/* 100 */     this.numInspected += 1;
/* 101 */     Account account = this.prov.get(Key.AccountBy.name, name);
/* 102 */     if (account == null)
/*     */     {
/* 104 */       this.printer.println("Account " + name + " not found!  Skipping.");
/* 105 */       return;
/*     */     }
/*     */     
/* 108 */     boolean modified = false;
/* 109 */     this.printer.println("# Account: " + account.getName());
/* 110 */     String[] addrs = account.getAllowFromAddress();
/* 111 */     this.printer.println("  Current value = " + StringUtil.join(", ", addrs));
/* 112 */     Set<String> remainingAddrs = new HashSet();
/* 113 */     for (String addr : addrs) {
/* 114 */       NamedEntry entry = lookupEntry(addr);
/* 115 */       if ((entry instanceof Account)) {
/* 116 */         if (!entry.getId().equalsIgnoreCase(account.getId())) {
/* 117 */           doGrant(entry, account, addr);
/* 118 */           modified = true;
/*     */         } else {
/* 120 */           this.printer.println("    - removing redundant address " + addr);
/* 121 */           modified = true;
/*     */         }
/* 123 */       } else if ((entry instanceof Group)) {
/* 124 */         doGrant(entry, account, addr);
/* 125 */         modified = true;
/*     */       } else {
/* 127 */         remainingAddrs.add(addr);
/*     */       }
/*     */     }
/*     */     
/* 131 */     if (modified) {
/* 132 */       Map<String, Object> attrsMap = new HashMap();
/* 133 */       if (!remainingAddrs.isEmpty()) {
/* 134 */         String[] remaining = (String[])remainingAddrs.toArray(new String[0]);
/* 135 */         attrsMap.put("zimbraAllowFromAddress", remaining);
/* 136 */         this.printer.println("  New value = " + StringUtil.join(", ", remaining));
/*     */       } else {
/* 138 */         attrsMap.put("zimbraAllowFromAddress", "");
/* 139 */         this.printer.println("  New value = <unset>");
/*     */       }
/* 141 */       this.prov.modifyAttrs(account, attrsMap, false, false);
/* 142 */       this.numFixed += 1;
/*     */     } else {
/* 144 */       this.printer.println("  No change needed");
/*     */     }
/* 146 */     this.printer.println();
/*     */   }
/*     */   
/*     */   private NamedEntry lookupEntry(String address) throws ServiceException {
/* 150 */     NamedEntry entry = null;
/* 151 */     String domain = EmailUtil.getValidDomainPart(address);
/* 152 */     if (domain != null) {
/* 153 */       Provisioning prov = Provisioning.getInstance();
/* 154 */       Domain internalDomain = prov.getDomain(Key.DomainBy.name, domain, true);
/* 155 */       if (internalDomain != null) {
/* 156 */         if (prov.isDistributionList(address)) {
/* 157 */           entry = prov.getGroupBasic(Key.DistributionListBy.name, address);
/*     */         } else {
/* 159 */           entry = prov.get(Key.AccountBy.name, address);
/*     */         }
/*     */       }
/*     */     }
/* 163 */     return entry;
/*     */   }
/*     */   
/*     */   private void doGrant(NamedEntry grantor, Account grantee, String address) throws ServiceException {
/* 167 */     String grantorTypeLabel = "account";
/* 168 */     String targetType = TargetType.account.getCode();
/* 169 */     String right = "sendAs";
/* 170 */     if ((grantor instanceof Group)) {
/* 171 */       grantorTypeLabel = "list";
/* 172 */       targetType = TargetType.group.getCode();
/* 173 */       right = "sendAsDistList";
/*     */     }
/* 175 */     this.prov.grantRight(targetType, TargetBy.name, grantor.getName(), GranteeType.GT_USER.getCode(), GranteeSelector.GranteeBy.name, grantee.getName(), null, right, null);
/*     */     
/* 177 */     this.printer.println("    - " + grantorTypeLabel + " " + grantor.getName() + " granting " + right + " right to " + grantee.getName());
/*     */     
/*     */ 
/* 180 */     String[] currAddrs = grantor.getMultiAttr("zimbraPrefAllowAddressForDelegatedSender");
/* 181 */     Set<String> addrsLowercase = new HashSet();
/* 182 */     for (String a : currAddrs)
/* 183 */       addrsLowercase.add(a.toLowerCase());
/*     */     boolean add;
/*     */     boolean add;
/* 186 */     if (addrsLowercase.isEmpty())
/*     */     {
/* 188 */       add = !grantor.getName().equalsIgnoreCase(address);
/*     */     }
/*     */     else {
/* 191 */       add = !addrsLowercase.contains(address.toLowerCase());
/*     */     }
/* 193 */     if (add) {
/* 194 */       Map<String, Object> attrs = new HashMap();
/* 195 */       StringUtil.addToMultiMap(attrs, "+zimbraPrefAllowAddressForDelegatedSender", address);
/* 196 */       this.prov.modifyAttrs(grantor, attrs);
/* 197 */       this.printer.println("    - address " + address + " added to " + "zimbraPrefAllowAddressForDelegatedSender" + " attribute of " + grantorTypeLabel + " " + grantor.getName());
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/account/ldap/upgrade/BUG_66387.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */